package com.jiang.daily.Q2349;
import java.util.*;
/**
 * @author Jiang
 * @version 1.0
 * @date 2025/09/17 11:27
 */
class NumberContainers {
    Map<Integer, Integer> container;
    Map<Integer, PriorityQueue<Integer>> heaps;

    public NumberContainers() {
        container = new HashMap<>();
        heaps = new HashMap<>();
    }

    public void change(int index, int number) {
        container.put(index, number);
        heaps.computeIfAbsent(number, k -> new PriorityQueue<>()).add(index);
    }

    public int find(int number) {
        PriorityQueue<Integer> heap = heaps.get(number);
        if (heap == null) return -1;

        while (!heap.isEmpty() && !container.get(heap.peek()).equals(number)) {
            heap.poll();
        }

        return heap.isEmpty() ? -1 : heap.peek();
    }
}

